<?
/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/
require_once("Database.php");
require_once("Utility.php");
require_once("Security.php");

class BVProfiles
{

	var $m_oDatabase;
	var $m_oSecurity;

	function BVProfiles()
	{
		$this->m_oDatabase = new Database();
		$this->m_oSecurity = new Security();
	}

	function Destruct()
	{
		$this->m_oDatabase->Destruct();
		$this->m_oSecurity->Destruct();
	}


	function CreateProfile($sProfileName, $sDefaultUrl, $bEmailOnlyOnFail)
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nEmailOnlyOnFail = ("true" == $bEmailOnlyOnFail) ? 1 : 0;
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$sSQL = "insert into Profiles (login_id, name, url, email_on_fail, profile_guid) values " .
			"(" . $nLoginID . ",'" . $this->m_oDatabase->EscapeString($sProfileName) . "'," .
			"'" . $this->m_oDatabase->EscapeString($sDefaultUrl) . "'," . $nEmailOnlyOnFail . ", uuid())";
		$this->m_oDatabase->ExecuteSQL($sSQL);
	}


	function GetProfiles()
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$sSQL = "select name, profile_guid " .
			"from Profiles " .
			"where login_id = " . $nLoginID . " " .
			"order by name";
		$aRows = $this->m_oDatabase->ExecuteSQLGetResults($sSQL);
		return $aRows;
	}

	function OpenProfile($sGuid)
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$sJson = "";
		$sSQL = "select name, url, email_on_fail " .
			"from Profiles " .
			"where login_id = " . $nLoginID . " " .
			"and profile_guid = '" . $this->m_oDatabase->EscapeString($sGuid) . "'";
		$nRows = $this->m_oDatabase->ExecuteSQL($sSQL);
		$aRow = $this->m_oDatabase->NextRow();
		$sJson = "{\"name\":\"" . $aRow["name"] . "\"," .
			"\"url\":\"" . $aRow["url"] . "\"," .
			"\"emailOnlyOnFail\":" . $aRow["email_on_fail"] . "}";
		return $sJson;
	}


	function EditProfile($sGuid, $sName, $sUrl, $sEmailOnlyOnFail)
	{
		if (false == $this->m_oSecurity->CheckCookie())
		{
			return;
		}
		$nLoginID = $this->m_oSecurity->GetLoginID();
		$nEmailOnlyOnFail = ("true" == $sEmailOnlyOnFail) ? 1 : 0;
		$sJson = "";
		$sSQL = "update Profiles set " .
			"name = '" . $this->m_oDatabase->EscapeString($sName) . "', " .
			"url = '" . $this->m_oDatabase->EscapeString($sUrl) . "', " .
			"email_on_fail = " . $nEmailOnlyOnFail . " " .
			"where profile_guid = '" . $this->m_oDatabase->EscapeString($sGuid) . "' " .
			"and login_id = " . $nLoginID;
		$this->m_oDatabase->ExecuteSQL($sSQL);
		$sJson = "{\"result\":true}";
		return $sJson;
	}

}



?>